package com.medical.goods.listen;

import com.medical.common.to.OrderTo;
import com.medical.goods.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RabbitListener(queues = {"order.release.order.queue"})
@Component
@Slf4j
public class OrderPayListen {
    @Autowired
    private OrderService orderService;
    @RabbitHandler
    public void listen(OrderTo orderTo, Channel channel, Message message) throws IOException {
        log.info("收到过期的订单，准备必单");
        try {
            orderService.closeOrder(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//mq确认机制
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);//mq拒绝重试机制
        }
    }
}
